package com.lianniu.mgr.baseService.post.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.organization.model.OrganizationMagVo;
import com.lianniu.mgr.baseService.organization.service.IOrganizationMagService;
import com.lianniu.mgr.baseService.post.model.Post2OrgVo;
import com.lianniu.mgr.baseService.post.model.PostVo;
import com.lianniu.mgr.baseService.post.service.IPostService;
import com.lianniu.mgr.pagination.PaginationInfo;

/**
 * @ClassName：PostRestController
 * @Description:岗位controller

 * @Since: 2016-3-24 11:03:56
 * @Version:1.0
 */
@Controller
@RequestMapping("/rest")
public class PostRestController {
  private final static Logger logger = Logger.getLogger(PostRestController.class);
  @Autowired
  private IPostService postService;
  @Autowired
  private IOrganizationMagService orgService;
  
  /**
   * @Description：岗位列表
   * @Param:输入参数的说明
   * @Return:返回值

   * @Date:2016-3-28 28:15:11
   */
  @RequestMapping(value = {"/postList"}, method = RequestMethod.GET)
  public @ResponseBody BaseResult postList(HttpServletRequest request,Post2OrgVo post2OrgVo,PaginationInfo paginationInf) {
    logger.info("PostRestController postList begin.");
    try {
      logger.info("getPostList begin.");
      BaseResult baseResult = postService.getPostList(post2OrgVo,paginationInf);
      logger.info("getPostList end.");
      logger.info("PostRestController postList end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("PostRestController postList error:" + e.getMessage());
      logger.error("PostRestController postList end.");
      return BaseResult.exception(e.getMessage());
    }
  }
   
   /**
    * @Description: 一级组织

    * @date 2016-3-28
    */
    @RequestMapping(value = {"/getPostOrg"}, method = RequestMethod.GET)
    public @ResponseBody BaseResult getPostOrg(HttpServletRequest request, OrganizationMagVo vo) {
      logger.info("PostRestController getPostOrg begin.");
      
      try {
        logger.info("getPostOrg begin.");
        vo.setParentId("0");
        BaseResult baseResult = orgService.getOrganizationLs(vo);
        logger.info("getPostOrg end.");
        logger.info("PostRestController getPostOrg end.");
        return baseResult;
      } catch (Exception e) {
        logger.error("PostRestController getPostOrg error:" + e.getMessage());
        logger.error("PostRestController getPostOrg end.");
        return BaseResult.exception(e.getMessage());
      }
    }
    
    /**
     * @Description: 一级以外组织

     * @date 2016-3-28
     */
     @RequestMapping(value = {"/getPostOrgNext"}, method = RequestMethod.GET)
     public @ResponseBody BaseResult getPostOrg2(HttpServletRequest request, OrganizationMagVo vo) {
       logger.info("PostRestController getPostOrgNext begin.");
       
       try {
         logger.info("getPostOrgNext begin.");
         vo.setParentId(request.getParameter("orgId"));
         BaseResult baseResult = orgService.getOrganizationLs(vo);
         logger.info("getPostOrgNext end.");
         logger.info("PostRestController getPostOrgNext end.");
         return baseResult;
       } catch (Exception e) {
         logger.error("PostRestController getPostOrgNext error:" + e.getMessage());
         logger.error("PostRestController getPostOrgNext end.");
         return BaseResult.exception(e.getMessage());
       }
     }
   
   /**
    * @Description: 添加岗位

    * @date 2016-3-28
    */
    @RequestMapping(value = {"/postAddSave"}, method = RequestMethod.GET)
    public @ResponseBody BaseResult postAddSave(HttpServletRequest request, 
  		  PostVo postVo, BindingResult result) {
      logger.info("PostRestController postAddSave begin.");
      logger.info("PostRestController postAddSave PostVo:" + JSON.toJSONString(postVo));
      
      String addUser = request.getParameter("addUser");
      postVo.setAddUser(addUser);
   
      try {
          logger.info("addPostInfo begin.");
          BaseResult baseResult = postService.addPostInfo(postVo);
          logger.info("addPostInfo end.");

          logger.info("PostRestController postAddSave end.");
          return baseResult;
        } catch (Exception e) {
          logger.error("PostRestController postAddSave error:" + e.getMessage());
          logger.error("PostRestController postAddSave end.");
          return BaseResult.exception(e.getMessage());
        } 
    }
    
   
   /**
    * @Description: 编辑岗位

    * @date 2016-3-28
    */
    @RequestMapping(value = {"/postEdit"}, method = RequestMethod.GET)
    public @ResponseBody BaseResult postEdit(HttpServletRequest request,
           Post2OrgVo post2OrgVo,BindingResult result) {
        try {
            logger.info("postEdit begin.");
            String id = request.getParameter("id");
            logger.info("id=" + id);
            post2OrgVo.setId(id);
            BaseResult baseResult = postService.getPost2OrgById(post2OrgVo);
            logger.info("postEdit end.");
            logger.info("PostRestController postEdit end.");
            return baseResult;
            
          } catch (Exception e) {
            logger.error("PostRestController postEdit error:" + e.getMessage());
            logger.error("PostRestController postEdit end.");
            return BaseResult.exception(e.getMessage());
          } 
    }
    

     
    /**
     * @Description: 编辑岗位

     * @date 2016-3-28
     */
     @RequestMapping(value = {"/postEditSave"}, method = RequestMethod.GET)
     public @ResponseBody BaseResult postEditSave(HttpServletRequest request,
         PostVo postVo,BindingResult result) {
  	    logger.info("PostRestController postEditSave begin.");
  	    logger.info("PostRestController postEditSave PostVo:" + JSON.toJSONString(postVo));
  	    
        String addUser = request.getParameter("addUser");
        postVo.setAddUser(addUser);
        
  	    try {
  	        logger.info("editPostInfo begin.");
  	      logger.info("postVo.orgid==" + postVo.getOrgId());
  	        BaseResult baseResult = postService.editPostInfo(postVo);
  	        logger.info("editPostInfo end.");

  	        logger.info("PostRestController postEditSave end.");
  	        return baseResult;
  	      } catch (Exception e) {
  	        logger.error("PostRestController postEditSave error:" + e.getMessage());
  	        logger.error("PostRestController postEditSave end.");
  	        return BaseResult.exception(e.getMessage());
  	      } 
     }
     
    /**
     * @Description：删除岗位
     * @Param:输入参数的说明
     * @Return:返回值

     * @Date:2016-3-29 21:15:11
     */
    @RequestMapping(value = {"/deletePost"}, method = RequestMethod.POST)
    public @ResponseBody BaseResult deletePost(HttpServletRequest request) {
      logger.info("PostRestController deletePost begin.");
      try {
        logger.info("deletePost begin.");
        String modifyUser = request.getParameter("modifyUser");
        BaseResult baseResult = postService.delPost(modifyUser,request.getParameter("ids").split(","));
        logger.info("deletePost end.");
        logger.info("PostRestController deletePost end.");
        return baseResult;
      } catch (Exception e) {
        logger.error("PostRestController deletePost error:" + e.getMessage());
        logger.error("PostRestController deletePost end.");
        return BaseResult.exception(e.getMessage());
      }
    }
    
    /**
     * @Description：复制岗位
     * @Param:输入参数的说明
     * @Return:返回值

     * @Date:2016-3-31 14:58:55
     */
    @RequestMapping(value = {"/copyPost"}, method = RequestMethod.GET)
    public @ResponseBody BaseResult copyPost(HttpServletRequest request) {
      logger.info("PostRestController copyPost begin.");
      String id = request.getParameter("id");
      try {
        logger.info("copyPost begin.");
        PostVo postVo = new PostVo();
        postVo.setId(id);
        BaseResult baseResult = postService.getPostInfoById(postVo);
        logger.info("copyPost end.");
        logger.info("PostRestController copyPost end.");
        return baseResult;
      } catch (Exception e) {
        logger.error("PostRestController copyPost error:" + e.getMessage());
        logger.error("PostRestController copyPost end.");
        return BaseResult.exception(e.getMessage());
      }
    }
    
    /**
     * @Description：复制岗位保存
     * @Param:输入参数的说明
     * @Return:返回值

     * @Date:2016-3-31 14:58:55
     */
    @RequestMapping(value = {"/saveCopyPost"}, method = RequestMethod.GET)
    public @ResponseBody BaseResult saveCopyPost(HttpServletRequest request,PostVo postVo) {
      logger.info("PostRestController saveCopyPost begin.");
      logger.info("PostRestController saveCopyPost PostVo:" + JSON.toJSONString(postVo));
      try {
        //测试用添加人
        String addUser = request.getParameter("addUser");
        postVo.setAddUser(addUser);
        logger.info("saveCopyPost begin.");
        BaseResult baseResult = postService.copyPostById(postVo);
        logger.info("saveCopyPost end.");
        logger.info("PostRestController saveCopyPost end.");
        return baseResult;
      } catch (Exception e) {
        logger.error("PostRestController saveCopyPost error:" + e.getMessage());
        logger.error("PostRestController saveCopyPost end.");
        return BaseResult.exception(e.getMessage());
      }
    }
}
